//
//  AccountBindVC.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/18.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "AccountBindVC.h"
#import "TypeDefineTableViewCell.h"
#import "CellTintModel.h"
#import "CellTintViewModel.h"

#import "ChangePhoneNumVC.h"
#import "ChangePwdVC.h"

@interface AccountBindVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *sectionOneArr;
@property (nonatomic, strong) NSMutableArray *sectionTwoArr;
@end


static NSString *bindCell = @"bindCell";
@implementation AccountBindVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"账号及绑定设置";
    self.view.backgroundColor = KLColor_C1;
    [self loadBindDataWithTint];
    [self loadTableView];
}

- (void)loadBindDataWithTint{
    self.dataArray = [CellTintViewModel returnBindInfoData];
    for (int i = 0; i < self.dataArray.count; i++) {
        CellTintModel *model = self.dataArray[i];
        if (i >= 0 && i <= 1) {
            [self.sectionOneArr addObject:model];
        }else if (i > 1 &&i< 5){
            [self.sectionTwoArr addObject:model];
        }
    }
}

// loadTableView
- (void)loadTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,KLScreenW,300*HEIGHT) style:UITableViewStyleGrouped];
    tableView.scrollEnabled = NO;
    tableView.backgroundColor = KLColor_C1;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    tableView.sectionHeaderHeight = 0;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KLScreenW, 10*HEIGHT)];
    tableView.tableHeaderView = headerView;
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

#pragma mark  ---- tableView 的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.sectionOneArr.count;
    }
    return self.sectionTwoArr.count;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    TypeDefineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:bindCell];
    
    if (indexPath.row == 0 && indexPath.section == 0) {
        if (!cell) {
            cell = [[TypeDefineTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:bindCell type:DefineCellTypeTintAndInSecond];
        }
        // 各自的配置
        cell.infoTint.text = @"157214***50";
        CellTintModel *model = self.sectionOneArr[indexPath.row];
        cell.model = model;
    }else if (indexPath.row == 1 && indexPath.section == 0){
        if (!cell) {
            cell = [[TypeDefineTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:bindCell type:DefineCellTypeInSecond];
        }
        CellTintModel *model = self.sectionOneArr[indexPath.row];
        cell.isShowLine = YES;  // 隐藏
        cell.model = model;
    }else {
        if (!cell) {
            cell = [[TypeDefineTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:bindCell type:DefineCellTypeSwitch];
        }
        
        CellTintModel *model = self.sectionTwoArr[indexPath.row];
        cell.model = model;
        if (indexPath.row == self.sectionTwoArr.count - 1) {
            cell.isShowLine = YES;
        }
        
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50*HEIGHT;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50*HEIGHT;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KLScreenW, 40*HEIGHT)];
        footView.backgroundColor = KLColor_C1;
//        UILabel *tintLb = [[UILabel alloc]initWithFrame:CGRectMake(15*WIDTH,11*HEIGHT , 100*WIDTH, 17*HEIGHT)];
        UILabel *tintLb = [[UILabel alloc]init];
        tintLb.text = @"社交平台账号绑定";
        tintLb.textColor = KLColor_B5;
        tintLb.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12*WIDTH];
        [footView addSubview:tintLb];
        [tintLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(footView);
            make.left.equalTo(footView).offset(15*WIDTH);
        }];
        return footView;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 40*HEIGHT;
    }
    return 0;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        ChangePhoneNumVC *changeNumVc = [[ChangePhoneNumVC alloc]init];
        [self.navigationController pushViewController:changeNumVc animated:YES];
    }else if (indexPath.section == 0 && indexPath.row == 1){
        ChangePwdVC *changePwdVc = [[ChangePwdVC alloc]init];
        [self.navigationController pushViewController:changePwdVc animated:YES];
    }
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)sectionOneArr{
    if (!_sectionOneArr) {
        _sectionOneArr = [NSMutableArray array];
    }
    return _sectionOneArr;
}

- (NSMutableArray *)sectionTwoArr{
    if (!_sectionTwoArr) {
        _sectionTwoArr = [NSMutableArray array];
    }
    return _sectionTwoArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
